﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AvatarHelper;
using Avatar.Model;
using System.Linq;

public partial class Admin_Industry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                list_Industry.DataSource = _adc.Industry.ToList();
                list_Industry.DataTextField = "name";
                list_Industry.DataValueField = "id";
                list_Industry.DataBind();
            }
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            var _name = string.IsNullOrEmpty(txt_IndustryName.Text) ? string.Empty : txt_IndustryName.Text;
            if (string.IsNullOrEmpty(_name)) throw new Exception("行业名称不能为空");

            using (var _adc = DBHelper.GetDataContext())
            {
                var _industry = _adc.Industry.Where(i => i.Name == _name).FirstOrDefault();
                if (_industry != null) throw new Exception("栏目名称已经存在");

                _industry = new Industry();
                _industry.Name = _name;

                _adc.Industry.InsertOnSubmit(_industry);
                _adc.SubmitChanges();

            }
            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "添加行业分类出错:" + _e.Message;
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            var _id = string.IsNullOrEmpty(list_Industry.SelectedValue) ? 0 : Convert.ToInt32(list_Industry.SelectedValue);
            using (var _adc = DBHelper.GetDataContext())
            {
                var _industry = _adc.Industry.Where(i => i.ID == _id).FirstOrDefault();
                if (_industry == null) throw new Exception("行业分类不存在");

                _adc.Industry.DeleteOnSubmit(_industry);
                _adc.SubmitChanges();
            }

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text="删除行业分类出错："+_e.Message;
        }
    }
}
